#include "TraderMaster.h"
#include <QtWidgets/QApplication>
#include "TrMarket.h"
#include "TrLogger.h"
#include "TrLoader.h"
#include <QSettings>
#include <chrono>
#include <QFileInfo>
#include <QDateTime>
#include <QDebug>
#include <QDialog>
#include <QTimer>
#include "TrTimer.hpp"


#pragma comment(lib, "TrUtils.lib")
#pragma comment(lib, "TrMarket.lib")
#pragma comment(lib, "TrManager.lib")
#pragma comment(lib, "TrLoader.lib")


class LoaderHandle : public TrObserver
{
public:
    LoaderHandle()
    {
    }
    ~LoaderHandle()
    {
        _release();
    }
    virtual void OnReceive(TrResponseID rId, void* wParam, void* lParam)
    {
        if (rId == TrResponseID::OnTdRspQryInstrumentUpdateSuccessed)
        {
            TrLogger::info("Loader update instrument successed ...");
            m_iLoaderUpdateTime = QDateTime::currentDateTime().toTime_t();
            _release();
        }
    }
    void _start() 
    {
        QSettings tmps = QSettings("conf/TraderMaster.ini", QSettings::IniFormat);
        QString strMasterRuleFile = tmps.value("Global/file", "conf/TraderMaster_se.ini").toString();

        QSettings conf(strMasterRuleFile, QSettings::IniFormat);
        if (conf.status() != QSettings::NoError)
        {
            TrLogger::error("Load TraderMaster.ini error.");
            exit(0);
        }

        QString frontAddr = conf.value("Loader/FrontAddr").toString();
        QString brokerId = conf.value("Loader/BrokerID").toString();
        QString userId = conf.value("Loader/UserID").toString();
        QString passWord = conf.value("Loader/Password").toString();
        QString appId = conf.value("Loader/AppID").toString();
        QString authCode = conf.value("Loader/AuthCode").toString();
        QString module = conf.value("Loader/Module").toString();
        int log_daily = conf.value("Loader/log_daily", 2).toInt();
        int log_stdout = conf.value("Loader/log_stdout", 2).toInt();

        TrConf_CTP c_ctpLoader{ 0 };

        strcpy(c_ctpLoader.FrontAddr, frontAddr.toStdString().c_str());
        strcpy(c_ctpLoader.BrokerID, brokerId.toStdString().c_str());
        strcpy(c_ctpLoader.UserID, userId.toStdString().c_str());
        strcpy(c_ctpLoader.Password, passWord.toStdString().c_str());
        strcpy(c_ctpLoader.AppID, appId.toStdString().c_str());
        strcpy(c_ctpLoader.AuthCode, authCode.toStdString().c_str());
        strcpy(c_ctpLoader.Module, module.toStdString().c_str());

        TrConf_Log c_log{ 0 };
        c_log.level_daily = static_cast<spdlog::level::level_enum>(log_daily);
        c_log.level_stdout = static_cast<spdlog::level::level_enum>(log_stdout);

        _release();

        m_pLoaderPtr = new TrLoader(c_ctpLoader, c_log);
    }
    void _release() 
    {
        if (m_pLoaderPtr != nullptr)
        {
            m_pLoaderPtr->release();
            m_pLoaderPtr = nullptr;
        }
    }
    bool _status()
    {
        QFileInfo fileInfo(CONTRACTS_FILE);
        uint get_time = fileInfo.lastModified().toTime_t();
        if (QDateTime::currentDateTime().toTime_t() - get_time > 3600 * 3)
        {
            return false;
        }
        return true;
    }

public:
    int m_iLoaderUpdateTime = 0;
    TrLoader* m_pLoaderPtr = nullptr;
};


int main(int argc, char* argv[])
{
    QApplication a(argc, argv);

    //LoaderHandle* pLoader = new LoaderHandle();
    //TrAttach(pLoader);
    //pLoader->_start();

    TraderMaster w;
    w.show();

    return a.exec();
}
